package com.icd.backend.controller;


import com.github.pagehelper.PageInfo;
import com.icd.backend.bean.dto.UserDTO;
import com.icd.backend.bean.dto.UserQueryDTO;
import com.icd.backend.bean.entity.User;
import com.icd.backend.srevice.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/users")
public class UserController {

    private final UserService userService;

    @PostMapping("/add")
    public User create(@RequestBody @Validated UserDTO userDTO) {
        return userService.create(userDTO);
    }

    @PutMapping("/{id}")
    public User update(@PathVariable String id, @RequestBody @Validated UserDTO userDTO) {
        return userService.update(id, userDTO);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        userService.delete(id);
    }

    @GetMapping("/{id}")
    public User getById(@PathVariable String id) {
        return userService.getById(id);
    }

    @GetMapping
    public PageInfo<User> list(UserQueryDTO queryDTO) {
        return userService.list(queryDTO);
    }
}
